﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class sellerOrder : System.Web.UI.Page
{
    CommonPage cm3 = new CommonPage();
    VLGDataContext _db8 = new VLGDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["currentItem"] = "yoursp";
        if (!IsPostBack)
        {
            Page.Title = "Trang cá nhân | Những đơn hàng bạn đã bán";
            if (cm3.IsAuthenticated == false)
            {
                Response.Redirect(ResolveClientUrl("~/accessDenied.aspx"));
            }
            else
            {
              
                fillDulieuLenGrid();
            }
        }
    }

    void fillDulieuLenGrid()
    {
        int idUs = cm3.Customer.idAcc;
        var totalOrder = (from o in _db8.Orders
                          where o.Gig.idAcc == idUs && o.CustomerActive == true && (o.CustomerCancel!=true&&o.SellerCancel!=true)&&(o.CustomerConfirm!=true)&&(o.CustomerActive==true)
                          select o).Count();
        //var totalOrder = (from o in _db8.Orders
        //                  where o.Gig.idAcc == idUs && o.CustomerActive == true
        //                  select o).Count();
        lblTotal.Text = totalOrder.ToString();

        //var totalCompleted = (from o in _db8.Orders
        //                      where o.Gig.idAcc == idUs && ((o.Status & (int)EnumOrderStatus.BuyerXacNhanHT) == (int)EnumOrderStatus.BuyerXacNhanHT)
        //                      && (o.Status & (int)EnumOrderStatus.SellerXacNhanHT) == (int)EnumOrderStatus.SellerXacNhanHT
        //                      orderby o.NgayDat
        //                      select o).Count();
        var totalCompleted = (from o in _db8.Orders
                              where o.Gig.idAcc == idUs && o.CustomerConfirm == true
                              && o.SellerConfirm == true && o.CustomerActive == true
                              select o).Count();
        lblCompleted.Text = totalCompleted.ToString();
        //var totalCancel = (from o in _db8.Orders
        //                   where o.Gig.idAcc == idUs && (((o.Status & (int)EnumOrderStatus.BuyerHuy) == (int)EnumOrderStatus.BuyerHuy)
        //                || (o.Status & (int)EnumOrderStatus.SellerHuy) == (int)EnumOrderStatus.SellerHuy)
        //                   orderby o.NgayDat
        //                   select o).Count();
        var totalCancel = (from o in _db8.Orders
                           where o.Gig.idAcc == idUs && ((o.CustomerCancel == true)
                        || (o.SellerCancel == true)) && o.CustomerActive == true
                           select o).Count();
        lblCanceled.Text = totalCancel.ToString();
        IQueryable<Order> gigCanhan = null;
        if (Request.QueryString["action"] != null)
        {
            if (Request.QueryString["action"].ToLower().Equals("completed"))
                //gigCanhan = from gg2 in _db8.Orders
                //            where gg2.Gig.idAcc == idUs && ((gg2.Status & (int)EnumOrderStatus.SellerXacNhanHT) == (int)EnumOrderStatus.SellerXacNhanHT && (gg2.Status & (int)EnumOrderStatus.BuyerXacNhanHT) == (int)EnumOrderStatus.BuyerXacNhanHT)
                //            orderby gg2.NgayDat
                //            select gg2;
                gigCanhan = from gg2 in _db8.Orders
                            where gg2.Gig.idAcc == idUs && (gg2.SellerConfirm == true && gg2.CustomerConfirm == true) &&gg2.CustomerActive==true
                            orderby gg2.NgayDat descending
                            select gg2;
            else if (Request.QueryString["action"].ToLower().Equals("cancelled"))
                gigCanhan = from gg2 in _db8.Orders
                            where gg2.Gig.idAcc == idUs && (gg2.CustomerCancel == true || gg2.SellerCancel == true) && gg2.CustomerActive == true
                            orderby gg2.NgayDat descending
                            select gg2;
            else gigCanhan = from gg2 in _db8.Orders
                             where gg2.Gig.idAcc == idUs && gg2.CustomerActive == true
                             orderby gg2.NgayDat descending
                             select gg2;
        }
        else

            gigCanhan = from gg2 in _db8.Orders
                        where gg2.Gig.idAcc == idUs && gg2.CustomerActive == true
                        orderby gg2.NgayDat descending
                        select gg2;
        AspNetPager1.RecordCount = gigCanhan.Count();
        GridView1.DataSource = gigCanhan.Skip(AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex-1)).Take(AspNetPager1.PageSize);
        GridView1.DataBind();
    }
    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        fillDulieuLenGrid();
    }
    protected void btnTiep2_Click(object sender, EventArgs e)
    {
        string noidung = TextBox1.Text.ToString().Trim();
        Response.Redirect(ResolveClientUrl("~/qlcongviec.aspx?op=add&to=") + noidung);
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
}
